import 'package:dio/dio.dart';
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';

void main() async {
  print("start");
  await Spider.getChapterContent("https://www.biquge.com.cn/book/32883/196851.html");
  print("stop");
}

class Spider {
  static getChapterContent(String chapterUrl) async {
    // 获取书籍的详情信息
    // todo 先查询数据库中是否存了
    Response resp = await Dio().get(chapterUrl);
    String data = resp.data;
    // 解析页面元素
    Document documnet = parse(data);
    // 解析book相关信息
    String chapterContent = documnet.getElementById("content").innerHtml;
    List<Element> pnChapters = documnet.querySelector(".bottem1").getElementsByTagName("a");
    String preChapterUrl = pnChapters[0].attributes["href"];
    String nextChapterUrl = pnChapters[2].attributes["href"];
    String chapterTitle = documnet.querySelector(".bookname").getElementsByTagName("h1")[0].innerHtml;

    Map result = {
      "chapterTitle": chapterTitle,
      "preChapterUrl": preChapterUrl,
      "nextChapterUrl": nextChapterUrl,
      "chapterContent": chapterContent
    };
    print(result);
    return result;
  }
}
